T<template>
  <div class="wrap">
    <el-form
      :inline="true"
      :model="ruleform"
      ref="from"
      :rules="rules"
      class="demo-form-inline ip-form"
    >
      <el-form-item label="设备序列号" prop="devices">
        <el-input v-model="ruleform.devices" message placeholder="不能输入中文"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="onSubmit('from')">添加</el-button>
        <el-button type="warning" plain @click="onReset('from')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
import { resetForm } from "@/utils/Tool";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    var checkNoChinese = (rule, value, callback) => {
      if (value) {
        if (new RegExp(/^[\u4E00-\u9FA5]+$/).test(value)) {
          callback(new Error("请不要输入中文！"));
        } else {
          callback();
        }
      }
      callback();
    };
    return {
      sn: "",
      ruleform: {
        devices: ""
      },
      rules: {
        devices: [
          { required: true, message: "请输入", trigger: "blur" },
          { validator: checkNoChinese, trigger: "blur" }
        ]
      }
    };
  },
  computed: {},
  created() {},
  mounted() {
    this.sn = this.$route.query.sn;
  },

  methods: {
    show() {
     // console.log(this.sn_checked);
    },
    //添加 提交
    onSubmit(formName) {
      var that = this;
      var devicesArr = [];
      devicesArr.push(that.ruleform.devices);
      that.$refs[formName].validate(valid => {
        if (valid) {
          postData("/adminweb/host/device/add", {
            sn: that.sn,
            devices: devicesArr
          })
            .then(res => {
              that.$message.success("添加成功！");
              //刷新table
              that.$store.commit(
                "setSubmitData",
                Math.floor(Math.random() * 10000000)
              );
              let datas = resetForm(that.ruleform, this.$refs[formName]);
              that.ruleform = datas;
            })
            .catch(res => {
              this.$message.error(res.code + "，" + res.msg);
            });
        } else {
          that.$message.error("添加失败");
          return false;
        }
      });
    },
    //重置
    onReset(val) {
      this.$refs[val].resetFields();
    }
  },
  watch: {}
};
</script>
<style scoped>
.wrap {
  /* background-color: #fff; */
  padding: 15px;
}
</style>
